//
//  AppDelegate.m
//  GiftTalk
//
//  Created by Wan on 16/3/22.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "AppDelegate.h"
#import "GTTabBarController.h"
#import "UMSocial.h"
#import "UMSocialSinaSSOHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "GTNewfeatureViewController.h"
#import <objc/NSObjCRuntime.h>

#define UMSocialAppKey @"56fa42d367e58ef6fc001ad7"
/// 切换控制器通知
NSString *GTSwitchRootViewControllerKey = @"GTSwitchRootViewControllerKey";

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)socailShare {
    // UMeng
    [UMSocialData setAppKey:@"56fa42d367e58ef6fc001ad7"];
    
    //打开调试log的开关
    //    [UMSocialData openLog:YES];
    
    /** 设置微信AppId，设置分享url */
    [UMSocialWechatHandler setWXAppId:@"wx86664943bfb71bd3" appSecret:@"62df04a82f2da0a57239e8e47123da26" url:@"www.thiswan.cn/social"];
    
    // 将在新浪微博注册的应用appkey、redirectURL替换下面参数，并在info.plist的URL Scheme中相应添加wb+appkey，如"wb3921700954"，详情请参考官方文档。
    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"1378843893"
                                              secret:@"30e74ccc7aa7f47b576248fee780eaf6"
                                         RedirectURL:@"http://www.thiswan.cn"];
    
    //设置分享到QQ空间的应用Id，和分享url 链接
    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
    //    //设置支持没有客户端情况下使用SSO授权
    [UMSocialQQHandler setSupportWebView:YES];
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 注册一个通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchRootViewController:) name:GTSwitchRootViewControllerKey object:nil];
    
    // 1. 创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    // 2. 设置窗口根控制器
    self.window.rootViewController = [self defaultController];
//    [[GTNewfeatureViewController alloc] init];
//    [[GTTabBarController alloc] init];
    
    // 3. 显示窗口
    [self.window makeKeyAndVisible];
    
    // UM
    [self socailShare];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

/**
 这里处理新浪微博SSO授权进入新浪微博客户端后进入后台，再返回原来应用
 */
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
    [UMSocialSnsService  applicationDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


/**
 用于获取默认界面
 */
- (UIViewController *)defaultController {
    
    return [self isNewUpdadte] ? [[GTNewfeatureViewController alloc] init] : [[GTTabBarController alloc] init];
}

- (BOOL)isNewUpdadte {
    // 1. 获取当前软件的版本号 -> info.plist
    NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    
    // 2. 获取以前的软件版本号 -> 从本地文件中读取（以前自己存储的）
    NSString *sandboxVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"CFBundleShortVersionString"];
    //        print("current = \(currentVersion) sanbox = \(sandboxVersion)")
    
    // 3. 比较当前版本和以前版本
    if ([currentVersion compare:sandboxVersion] == NSOrderedDescending)
    {
        // 存储当前最新的版本号
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"CFBundleShortVersionString"];
        
        
        return true;
    }
    return false;
}

- (void)switchRootViewController:(NSNotification *)notify {
    if ([notify.name isEqualToString:GTSwitchRootViewControllerKey]) {
        self.window.rootViewController = [[GTTabBarController alloc] init];
    } else {
        self.window.rootViewController = [[GTNewfeatureViewController alloc] init];
    }
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
